package com.lxhll.personnel.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lxhll.personnel.core.CurrentEmployee;
import com.lxhll.personnel.core.ResponseData;
import com.lxhll.personnel.entity.Employee;
import com.lxhll.personnel.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * (Employee)表控制层
 *
 * @author makejava
 * @since 2021-07-18 15:06:39
 */
@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    /**
     * 服务对象
     */
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private EmployeeRoleService employeeRoleService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private PositionService positionService;
    @Autowired
    private ProbationService probationService;
    /**
     * 查询员工，根据所属部门，岗位。。。并分页
     */
    @PostMapping("/list")
    public ResponseData list(@RequestBody Map<String ,Object> params){
        //提取查询参数与分页
        System.out.println("当前登录员工：");
        log.info("查询参数:"+params);
        //查询员工列表,可以按照员工编号，姓名，身份证，岗位，部门等等多个条件查询
        IPage<Employee> employeePage=employeeService.pageEmployee(params);
        //返回
        return ResponseData.ok(employeePage);
    }

    /**
     * 查询试用期员工，根据所属部门，岗位。。。并分页
     */
    @PostMapping("/probation_list")
    public ResponseData probation_list(@RequestBody Map<String ,Object> params){
        //提取查询参数与分页
        log.info("查询参数:"+params);
        //查询试用期员工列表，自定义sql
        IPage<Map<String,Object>> employeePage=employeeService.queryEmployee(params);
        //返回
        return ResponseData.ok(employeePage);
    }

    
    /**
     * 查询单个员工信息
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public ResponseData info(@PathVariable("id") Integer id){
        Employee employee = employeeService.getEmployeeById(id);
        return ResponseData.ok(employee);
    }

    /**
     * 删除员工，可以批量
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    public ResponseData deleteEmployee(@RequestBody Integer[] ids){
        employeeService.removeEmployeeByIds(ids);
        return ResponseData.ok();
    }

    /**
     * 员工信息修
     * @param employee
     * @return
     */
    @PostMapping(value="/update")
    public ResponseData update(@Validated @RequestBody Employee employee) {
        employeeService.updateEmployeeById(employee);
        return ResponseData.ok();
    }

    /**
     * 员工信息修改，如果是实习员工，更新试用期信息
     * @param employee
     * @return
     */
    @PostMapping(value="/update/probation")
    public ResponseData updateProbation(@Validated @RequestBody Employee employee) {
        employeeService.updateProbationEmployeeById(employee);
        return ResponseData.ok();
    }

    /**
     * 增加员工
     */
    @PostMapping("/save")
    public ResponseData add(@Validated @RequestBody Employee employee){
        //插入员工
        employeeService.saveEmployee(employee);
        return ResponseData.ok();
    }

    /**
     * 获取当前登录的员工
     * @param employee
     * @return
     */
    @GetMapping("/getme")
    public ResponseData getMe(@CurrentEmployee Employee employee){
        return ResponseData.ok(employee);
    }

}
